package com.wt.chat.config;

import com.alibaba.fastjson.JSON;
import com.wt.chat.response.ResponseResult;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletResponse;

/**
 * @description
 * @author: wangtao
 * @date:15:43 2019/11/12
 * @email:taow02@jumei.com
 */
public class ChatReturnValueHandler implements HandlerMethodReturnValueHandler {
	@Override
	public boolean supportsReturnType(MethodParameter returnType) {
		return returnType.getParameterType().isAssignableFrom(ResponseResult.class);
	}

	@Override
	public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest) throws Exception {
		mavContainer.setRequestHandled(true);
		HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().println(JSON.toJSONStringWithDateFormat(returnValue,"yyyy-MM-dd HH:mm:ss"));
	}
}
